package com.planet.stockanalysis.server.stockutils;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.planet.stockanalysis.server.entity.Index;
import com.planet.stockanalysis.server.entity.Stock;
import com.planet.stockanalysis.server.service.StockService;
import com.planet.stockanalysis.utils.Utils;

/**
 * 解析股票的模板
 * 属性名字、index键值对
 * @author planet
 *
 */
public class StockParser {
	static SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-ddHH:mm:ss");
	public static final int MAX_SINAJS_GRAB_COUNT = 800;
	/*
	 ------------------ 股票字段 --------------
	 */
	public static final String STOCK_NAME = "name";
	public static final String STOCK_CODE = "code";
	/**
	 * 成交量
	 */
	public static final String STOCK_TURN_OVER ="turnOver";
	/**
	 * 成交额
	 */
	public static final String STOCK_VOLUME = "volume";
	/**
	 * 涨跌额
	 */
	public static final String STOCK_CHANGE_AMOUNT = "changeAmount";
	/**
	 * 涨跌幅(%)
	 */
	public static final String STOCK_CHANGE_RANGE = "changeRange";
	/**
	 * 竞买价（即“买一”报价）
	 */
	public static final String STOCK_BID_PRICE = "bidPrice";
	/**
	 * 竞卖价（即“卖一”报价）
	 */
	public static final String STOCK_ASK_PRICE = "askPrice";
	/**
	 * 委比（%）
	 */
	public static final String STOCK_COMMISSION_RADIO = "commissionRadio";
	/**
	 * 最新价
	 */
	public static final String STOCK_NEW_PRICE = "newPrice";
	/**
	 * 昨收盘
	 */
	public static final String STOCK_CLOSING_PRICE = "closingPrice";
	/**
	 * 今开盘
	 */
	public static final String STOCK_OPENING_PRICE = "openingPrice";
	/**
	 * 最低价
	 */
	public static final String STOCK_BOTTOM_PRICE = "bottomPrice";
	/**
	 * 最高价
	 */
	public static final String STOCK_TOP_PRICE = "topPrice";

	public static final String STOCK_BUY1_AMOUNT = "buy1Amount";
	public static final String STOCK_BUY2_AMOUNT = "buy2Amount";
	public static final String STOCK_BUY3_AMOUNT = "buy3Amount";
	public static final String STOCK_BUY4_AMOUNT = "buy4Amount";
	public static final String STOCK_BUY5_AMOUNT = "buy5Amount";
	public static final String STOCK_SELL1_AMOUNT = "sell1Amount";
	public static final String STOCK_SELL2_AMOUNT = "sell2Amount";
	public static final String STOCK_SELL3_AMOUNT = "sell3Amount";
	public static final String STOCK_SELL4_AMOUNT = "sell4Amount";
	public static final String STOCK_SELL5_AMOUNT = "sell5Amount";
	public static final String STOCK_BUY1 = "buy1";
	public static final String STOCK_BUY2 = "buy2";
	public static final String STOCK_BUY3 = "buy3";
	public static final String STOCK_BUY4 = "buy4";
	public static final String STOCK_BUY5 = "buy5";
	public static final String STOCK_SELL1 = "sell1";
	public static final String STOCK_SELL2 = "sell2";
	public static final String STOCK_SELL3 = "sell3";
	public static final String STOCK_SELL4 = "sell4";
	public static final String STOCK_SELL5 = "sell5";
	public static final String STOCK_DATE = "date";
	public static final String STOCK_TIME = "time";
	public static final String STOCK_DATE_TIME = "date_time";

	//指数
	public static final String INDEX_NAME = "name";
	/**
	 * 当前点数
	 */
	public static final String INDEX_POINTS = "points";
	/**
	 * 当前价格
	 */
	public static final String INDEX_PRICE = "price";
	/**
	 * 涨跌率
	 */
	public static final String INDEX_CHANGE_RATE = "changeRate";
	/**
	 * 成交量（手）
	 */
	public static final String INDEX_TURN_OVER = "turnOver";
	/**
	 * 成交额（万元）
	 */
	public static final String INDEX_VOLUME = "volume";

	/**
	 * 股票
	 */
	public static final Map<String, Integer> SinaJSTemplate_STOCK = new HashMap<String, Integer>();
	/**
	 * 指数
	 */
	public static final Map<String, Integer> SinaJSTemplate_INDEX = new HashMap<String, Integer>();

	static{
		/*
		 浙江世宝,
		 14.54,
		 14.54,
		 15.99,
		 15.99,
		 14.08,
		 15.99,
		 0.00,
		 4736661,
		 72365253.87,
		 236982,15.99,
		 8700,15.98,
		 200,15.97,
		 200,15.94,
		 12345,15.93,
		 0,0.00
		 ,0,0.00
		 ,0,0.00
		 ,0,0.00
		 ,0,0.00
		 ,2013-06-28,
		 15:05:53,
		 00
		 */
		SinaJSTemplate_STOCK.put(STOCK_NAME, 0);
		SinaJSTemplate_STOCK.put(STOCK_OPENING_PRICE, 1);
		SinaJSTemplate_STOCK.put(STOCK_CLOSING_PRICE, 2);
		SinaJSTemplate_STOCK.put(STOCK_NEW_PRICE, 3);
		SinaJSTemplate_STOCK.put(STOCK_TOP_PRICE, 4);
		SinaJSTemplate_STOCK.put(STOCK_BOTTOM_PRICE, 5);
		SinaJSTemplate_STOCK.put(STOCK_BID_PRICE, 6);
		SinaJSTemplate_STOCK.put(STOCK_ASK_PRICE, 7);
		SinaJSTemplate_STOCK.put(STOCK_TURN_OVER, 8);
		SinaJSTemplate_STOCK.put(STOCK_VOLUME, 9);
		SinaJSTemplate_STOCK.put(STOCK_BUY1_AMOUNT, 10);
		SinaJSTemplate_STOCK.put(STOCK_BUY1, 11);
		SinaJSTemplate_STOCK.put(STOCK_BUY2_AMOUNT, 12);
		SinaJSTemplate_STOCK.put(STOCK_BUY2, 13);
		SinaJSTemplate_STOCK.put(STOCK_BUY3_AMOUNT, 14);
		SinaJSTemplate_STOCK.put(STOCK_BUY3, 15);
		SinaJSTemplate_STOCK.put(STOCK_BUY4_AMOUNT, 16);
		SinaJSTemplate_STOCK.put(STOCK_BUY4, 17);
		SinaJSTemplate_STOCK.put(STOCK_BUY5_AMOUNT, 18);
		SinaJSTemplate_STOCK.put(STOCK_BUY5, 19);
		SinaJSTemplate_STOCK.put(STOCK_SELL1_AMOUNT, 20);
		SinaJSTemplate_STOCK.put(STOCK_SELL1, 21);
		SinaJSTemplate_STOCK.put(STOCK_SELL2_AMOUNT, 22);
		SinaJSTemplate_STOCK.put(STOCK_SELL2, 23);
		SinaJSTemplate_STOCK.put(STOCK_SELL3_AMOUNT, 24);
		SinaJSTemplate_STOCK.put(STOCK_SELL3, 25);
		SinaJSTemplate_STOCK.put(STOCK_SELL4_AMOUNT, 26);
		SinaJSTemplate_STOCK.put(STOCK_SELL4, 27);
		SinaJSTemplate_STOCK.put(STOCK_SELL5_AMOUNT, 28);
		SinaJSTemplate_STOCK.put(STOCK_SELL5, 29);
		SinaJSTemplate_STOCK.put(STOCK_DATE, 30);
		SinaJSTemplate_STOCK.put(STOCK_TIME, 31);

		SinaJSTemplate_INDEX.put(INDEX_NAME, 0);
		SinaJSTemplate_INDEX.put(INDEX_POINTS, 1);
		SinaJSTemplate_INDEX.put(INDEX_PRICE, 2);
		SinaJSTemplate_INDEX.put(INDEX_CHANGE_RATE, 3);
		SinaJSTemplate_INDEX.put(INDEX_TURN_OVER, 4);
		SinaJSTemplate_INDEX.put(INDEX_VOLUME, 5);
	}


	public static Stock getStockFromSinaJS(String code){
		Stock stock = null;


		return stock;
	}

	/**
	 * 批量从SINAJS接口获取数据
	 * 列表长度不能超过MAX_SINAJS_GRAB_COUNT
	 * @param codes
	 * @return
	 * @throws Exception 
	 */
	public static List<Stock> getStocksFromSinaJS(List<String> codes,
			StockService stockService) throws Exception{
		List<Stock> stocks = new ArrayList<Stock>();
		if(codes.size() > MAX_SINAJS_GRAB_COUNT){
			throw new Exception("数量大于 MAX_SINAJS_GRAB_COUNT");
		}
		String url = "http://hq.sinajs.cn/list=";
		for(int i=0; i<codes.size(); i++){
			if(i==codes.size()-1){
				url+=codes.get(i);
			}else{
				url+=codes.get(i)+",";
			}
		}
		System.out.println("url="+url);
		String data = Utils.readString(url, "GBK");
		System.out.println("data="+data);
		String[] lines = data.split(";");
		System.out.println(lines.length);
		for(int i=0; i<codes.size(); i++){
			String code = codes.get(i);
			String line = lines[i];
			line = line.replaceFirst("var hq_str_"+code+"=\"", "");
			line = line.substring(0, line.length()-1);
			System.out.println("line="+line);
			String[] info = line.split(",");
			Stock s = new Stock();
			s.setName(info[SinaJSTemplate_STOCK.get(STOCK_NAME)]);
			s.setAskPrice(Double.parseDouble(info[SinaJSTemplate_STOCK.get(STOCK_ASK_PRICE)]));
			s.setBidPrice(Double.parseDouble(info[SinaJSTemplate_STOCK.get(STOCK_BID_PRICE)]));
			s.setBottomPrice(Double.parseDouble(info[SinaJSTemplate_STOCK.get(STOCK_BOTTOM_PRICE)]));
			//s.setChangeAmount(Double.parseDouble(info[SinaJSTemplate_STOCK.get(STOCK_CHANGE_AMOUNT)]));
			//s.setChangeRange(Double.parseDouble(info[SinaJSTemplate_STOCK.get(STOCK_CHANGE_RANGE)]));
			s.setClosingPrice(Double.parseDouble(info[SinaJSTemplate_STOCK.get(STOCK_CLOSING_PRICE)]));
			s.setCode(stockService.findStockCode(code));
			String date = info[SinaJSTemplate_STOCK.get(STOCK_DATE)];
			String time = info[SinaJSTemplate_STOCK.get(STOCK_TIME)];
			s.setDate(sdf.parse(date+time));
			s.setGrabDate(new Date());
			s.setNewPrice(Double.parseDouble(info[SinaJSTemplate_STOCK.get(STOCK_NEW_PRICE)]));
			s.setOpeningPrice(Double.parseDouble(info[SinaJSTemplate_STOCK.get(STOCK_OPENING_PRICE)]));
			s.setTopPrice(Double.parseDouble(info[SinaJSTemplate_STOCK.get(STOCK_TOP_PRICE)]));
			s.setTurnOver(Integer.parseInt(info[SinaJSTemplate_STOCK.get(STOCK_TURN_OVER)]));
			s.setVolume(Double.parseDouble(info[SinaJSTemplate_STOCK.get(STOCK_VOLUME)]));
			
			s.setBuy1(Double.parseDouble(info[SinaJSTemplate_STOCK.get(STOCK_BUY1)]));
			s.setBuy1Amount(Integer.parseInt(info[SinaJSTemplate_STOCK.get(STOCK_BUY1_AMOUNT)]));
			s.setBuy2(Double.parseDouble(info[SinaJSTemplate_STOCK.get(STOCK_BUY2)]));
			s.setBuy2Amount(Integer.parseInt(info[SinaJSTemplate_STOCK.get(STOCK_BUY2_AMOUNT)]));
			s.setBuy3(Double.parseDouble(info[SinaJSTemplate_STOCK.get(STOCK_BUY3)]));
			s.setBuy3Amount(Integer.parseInt(info[SinaJSTemplate_STOCK.get(STOCK_BUY3_AMOUNT)]));
			s.setBuy4(Double.parseDouble(info[SinaJSTemplate_STOCK.get(STOCK_BUY4)]));
			s.setBuy4Amount(Integer.parseInt(info[SinaJSTemplate_STOCK.get(STOCK_BUY4_AMOUNT)]));
			s.setBuy5(Double.parseDouble(info[SinaJSTemplate_STOCK.get(STOCK_BUY5)]));
			s.setBuy5Amount(Integer.parseInt(info[SinaJSTemplate_STOCK.get(STOCK_BUY5_AMOUNT)]));
			
			s.setSell1(Double.parseDouble(info[SinaJSTemplate_STOCK.get(STOCK_SELL1)]));
			s.setSell2(Double.parseDouble(info[SinaJSTemplate_STOCK.get(STOCK_SELL2)]));
			s.setSell3(Double.parseDouble(info[SinaJSTemplate_STOCK.get(STOCK_SELL3)]));
			s.setSell4(Double.parseDouble(info[SinaJSTemplate_STOCK.get(STOCK_SELL4)]));
			s.setSell5(Double.parseDouble(info[SinaJSTemplate_STOCK.get(STOCK_SELL5)]));
			s.setSell1Amount(Integer.parseInt(info[SinaJSTemplate_STOCK.get(STOCK_SELL1_AMOUNT)]));
			s.setSell2Amount(Integer.parseInt(info[SinaJSTemplate_STOCK.get(STOCK_SELL2_AMOUNT)]));
			s.setSell3Amount(Integer.parseInt(info[SinaJSTemplate_STOCK.get(STOCK_SELL3_AMOUNT)]));
			s.setSell4Amount(Integer.parseInt(info[SinaJSTemplate_STOCK.get(STOCK_SELL4_AMOUNT)]));
			s.setSell5Amount(Integer.parseInt(info[SinaJSTemplate_STOCK.get(STOCK_SELL5_AMOUNT)]));
			stocks.add(s);
		}
		System.out.println("stocks="+stocks.size());
		return stocks;
	}

	public static Index getIndexFromSinaJS(String code){
		Index index = null;


		return index;
	}

	public static void main(String args[]){
		ClassPathXmlApplicationContext ctx = 
				new ClassPathXmlApplicationContext("spring-cfg.xml"); 
		StockService stockService = (StockService)ctx.getBean("stockService");
		long t = System.currentTimeMillis();
		List<String> codes = new ArrayList<String>();
		for(int i=0; i<100; i++){
			codes.add("sz002703");
		}
		try {
			List<Stock> stocks = StockParser.getStocksFromSinaJS(codes, stockService);
			for(Stock s : stocks){
				System.out.println(s.getName());
				System.out.println(s.getDate());
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		System.out.println(System.currentTimeMillis()-t);
	}
}
